/*
 * 功能    : 操作表单验证、提交
 * 编辑时间：2014-03-31
 * 作者    ：Arvin.chen@zkteco.com
 */

$(document).on( "pagecreate", "#main_page", function() { //更改日期文本框
    var picker=$(".date",this);
    for(var i=0;i<picker.length;i++){
        var p=picker[i];
        var val=$(p).val();
        var date='';
        var time='';
        if(val!=''){
          dt=val.split(' ');
          date=dt[0];
          time=dt[1];
        }
        var did=$(p).attr('id')+'date';
        var tid=$(p).attr('id')+'time';
        $(p).replaceWith(''
        +'<input data-theme="b" belong='+$(p).attr('name')+' d_type="date" class="required" name="'+did+'" value="'+date+'" placeholder='+gettext("请选择日期")+' id='+$(p).attr('id')+' type="text" data-role="datebox" data-options={"mode":"datebox"} readonly="readonly">'
        +'<input data-theme="b" belong='+$(p).attr('name')+' d_type="time" class="required" name="'+tid+'" value="'+time.substr(0,5)+'" placeholder='+gettext("请选择时间")+' id='+$(p).attr('id')+' type="text" data-role="datebox" data-options={"mode":"timebox"} readonly="readonly">'
        +'');
    }
});
$(document).on("pageinit",function(){ //绑定按钮事件
   var app_label=$("#app_label").val();
   var model_app=$("#model_app").val();
   var model_name=$("#model_name").val();
   var detail=$("#detail").val();
   var key=$("#key").val();
   $("#OK").unbind().click(function(){
       var dtime=[]
       var params=[]
       $("#main_content").find("select,input").each(function(){
           var pn=$(this).attr("name");
           var pt=$(this).attr("d_type");
           var pv=$(this).val();
           var dt=new Date();
           if(pt=='date' || pt=='time'){
               dtime.push(pv)
               if(dtime.length==2){
                  var dpn=$(this).attr("belong");
                  var time=dtime.join(' ');
//                  params.push({name:dpn,value:(time+":"+dt.getSeconds())})
                  params.push({name:dpn,value:(time+":00")})
                  dtime=[];
               }
           }else{
              params.push({name:pn,value:pv})
           }
      })
       $form= $("#data_form");
       $("#error_info").empty();
       if($form.valid()){
            if(typeof(check_audit_before_submit)!= "undefined"){
                   if(!check_audit_before_submit()){
                       return;
                     };
               }
    
             $.ajax({
                url:"/"+app_label+"/"+model_app+"/"+model_name+"/_op_/"+detail+"/?K="+key,
                data:params,
                type:"POST",
                cache:false,
                dataType:"html",
                success:function(callback){
                  if(callback.indexOf('{ Info:"OK" }')!=-1){
                    var op_lower=detail.toLowerCase()
                    if( op_lower.indexOf('delete')!=-1){
                       $('#home').trigger("click");
                    }else{
                       $('#BACK').trigger("click");
                    }
                 }else{
                       var $error=$($.parseHTML(callback)).find("ul.errorlist");
                       if($error.length!=0){
                           $("#error_info").html($error.eq(0)).hide().show(100);
                       }else{
                           $("#error_info").html(callback);
                       }
                 }
               },
               error:function(callback){
                   $("#error_info").html(gettext("服务器出错！"))
               }
             });
        }else{
          $form.find("input.error").eq(0).focus()
		  $("#error_info").append('<ul class="errorlist"><li>'+gettext("这个字段是必填项。")+'</li></ul>');
       }
  });
  /*
   * 选择审批人的渲染
   */
  var url = "/wap/select_parent_each_dept_top_personnel/?skip=True";
  var AuditUserID = $("#select[id=id_AuditUserID]");
  if (typeof(AuditUserID)!='undefined'){
  	$.ajax({
  	    type:"POST",
  	    url:encodeURI(url),
  	    dataType:"json",
  	    success:function(msg){
  	        var html_list = [];
  	        html_list.push("<option value=''>---------</option>");
  	        data=msg['dict_rows'];
  	        if(data.length !=0){
  	            for(var i=0;i<data.length;i++){
                    row_data=data[i];
                    if (row_data['isParent']){
                        var row = "<option value='' disabled=true>" + row_data['name']+"</option>";
                    }else{
  	                    var row = "<option value='" + row_data['id'] + "'>" + row_data['name']+"</option>";
                    }
  	                html_list.push(row);
  	            }
  	        }
  	        $("select[id=id_AuditUserID]").html(html_list.join(""));
  	    }
  	});
  }
});
